package com.backstage.webSocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    //https://blog.csdn.net/runbat/article/details/80985944
    //https://blog.csdn.net/zmx729618/article/details/78584633

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry){
        //egisterWebSocketHandlers：这个方法是向spring容器注册一个handler地址，我把他理解成requestMapping
        //addInterceptors：拦截器，当建立websocket连接的时候，我们可以通过继承spring的HttpSessionHandshakeInterceptor来搞事情。
        //setAllowedOrigins：跨域设置，*表示所有域名都可以，不限制， 域包括ip：port, 指定*可以是任意的域名，不加的话默认localhost+本服务端口
        //withSockJS： 这个是应对浏览器不支持websocket协议的时候降级为轮询的处理。

        registry.addHandler(webSocketHandler(),"/websocket/socketServer").addInterceptors(new ChatHandshakeInterceptor()).setAllowedOrigins("*");
        //System.out.println("registed! - 注册");
        registry.addHandler(webSocketHandler(),"/sockjs/socketServer").setAllowedOrigins("*").addInterceptors(new ChatHandshakeInterceptor()).withSockJS();
    }
    @Bean
    public TextWebSocketHandler webSocketHandler(){
        return new ChatMessageHandler();
    }

}
